package com.linkeleee.core.exception;

import com.linkeleee.core.common.EMessage;
import com.linkeleee.core.exception.BizException;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * 断言类
 * @author Shuang.Li.Li
 */
public class Assert {

    public static void isTrue(boolean expression) {
        if (!expression) {
            throw new BizException();
        }
    }

    public static void isTrue(boolean expression, EMessage eMessage) {
        if (!expression) {
            throw new BizException(eMessage.getCode(), eMessage.getMsg(), eMessage.getDesc());
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new BizException(message);
        }
    }

    public static void isTrue(boolean expression, int code, String message) {
        if (!expression) {
            throw new BizException(code, message);
        }
    }

    public static void isTrue(boolean expression, String template, Object... params) {
        if (!expression) {
            throw new BizException(format(template, params));
        }
    }

    public static void isTrue(boolean expression, int code, String template, Object... params) {
        if (!expression) {
            throw new BizException(code, format(template, params));
        }
    }

    public static <T> T notNull(T object) {
        if (object == null) {
            throw new BizException();
        } else {
            return object;
        }
    }

    public static <T> T notNull(T object, EMessage eMessage) {
        if (object == null) {
            throw new BizException(eMessage.getCode(), eMessage.getMsg(), eMessage.getDesc());
        } else {
            return object;
        }
    }

    public static <T> T notNull(T object, String errorMsg) {
        if (object == null) {
            throw new BizException(errorMsg);
        } else {
            return object;
        }
    }

    public static <T> T notNull(T object, int code, String errorMsg) {
        if (object == null) {
            throw new BizException(code, errorMsg);
        } else {
            return object;
        }
    }

    public static <T> T notNull(T object, String template, Object... params) {
        if (object == null) {
            throw new BizException(format(template, params));
        } else {
            return object;
        }
    }

    public static <T> T notNull(T object, int code, String template, Object... params) {
        if (object == null) {
            throw new BizException(code, format(template, params));
        } else {
            return object;
        }
    }

    public static void hasLength(String text) {
        if (StringUtils.isEmpty(text)) {
            throw new BizException();
        }
    }

    public static void hasLength(String text, EMessage eMessage) {
        if (StringUtils.isEmpty(text)) {
            throw new BizException(eMessage.getCode(), eMessage.getMsg(), eMessage.getDesc());
        }
    }

    public static void hasLength(String text, String errorMsg) {
        if (StringUtils.isEmpty(text)) {
            throw new BizException(errorMsg);
        }
    }

    public static void hasLength(String text, int code, String errorMsg) {
        if (StringUtils.isEmpty(text)) {
            throw new BizException(code, errorMsg);
        }
    }

    public static void hasLength(String text, String template, Object... params) {
        if (StringUtils.isEmpty(text)) {
            throw new BizException(format(template, params));
        }
    }

    public static void hasLength(String text, int code, String template, Object... params) {
        if (StringUtils.isEmpty(text)) {
            throw new BizException(code, format(template, params));
        }
    }

    public static void notEmpty(Object[] array) {
        if (array == null || array.length == 0) {
            throw new BizException();
        }
    }

    public static void notEmpty(Object[] array, EMessage eMessage) {
        if (array == null || array.length == 0) {
            throw new BizException(eMessage.getCode(), eMessage.getMsg(), eMessage.getDesc());
        }
    }

    public static void notEmpty(Object[] array, String errorMsg) {
        if (array == null || array.length == 0) {
            throw new BizException(errorMsg);
        }
    }

    public static void notEmpty(Object[] array, int code, String errorMsg) {
        if (array == null || array.length == 0) {
            throw new BizException(code, errorMsg);
        }
    }

    public static void notEmpty(Object[] array, String template, Object... params) {
        if (array == null || array.length == 0) {
            throw new BizException(format(template, params));
        }
    }

    public static void notEmpty(Object[] array, int code, String template, Object... params) {
        if (array == null || array.length == 0) {
            throw new BizException(code, format(template, params));
        }
    }

    public static void notEmpty(Collection collection) {
        if (collection == null || collection.isEmpty()) {
            throw new BizException();
        }
    }

    public static void notEmpty(Collection collection, EMessage eMessage) {
        if (collection == null || collection.isEmpty()) {
            throw new BizException(eMessage.getCode(), eMessage.getMsg(), eMessage.getDesc());
        }
    }

    public static void empty(Collection collection, EMessage eMessage){
        if(!Objects.isNull(collection)&&!collection.isEmpty()){
            throw new BizException(eMessage.getCode(), eMessage.getMsg(), eMessage.getDesc());
        }
    }

    public static void notEmpty(Collection collection, String errorMsg) {
        if (collection == null || collection.isEmpty()) {
            throw new BizException(errorMsg);
        }
    }

    public static void notEmpty(Collection collection, int code, String errorMsg) {
        if (collection == null || collection.isEmpty()) {
            throw new BizException(code, errorMsg);
        }
    }

    public static void notEmpty(Collection collection, String template, Object... params) {
        if (collection == null || collection.isEmpty()) {
            throw new BizException(format(template, params));
        }
    }

    public static void notEmpty(Collection collection, int code, String template, Object... params) {
        if (collection == null || collection.isEmpty()) {
            throw new BizException(code, format(template, params));
        }
    }

    public static void notEmpty(Map map) {
        if (map == null || map.isEmpty()) {
            throw new BizException();
        }
    }

    public static void notEmpty(Map map, EMessage eMessage) {
        if (map == null || map.isEmpty()) {
            throw new BizException(eMessage.getCode(), eMessage.getMsg(), eMessage.getDesc());
        }
    }

    public static void notEmpty(Map map, String errorMsg) {
        if (map == null || map.isEmpty()) {
            throw new BizException(errorMsg);
        }
    }

    public static void notEmpty(Map map, int code, String errorMsg) {
        if (map == null || map.isEmpty()) {
            throw new BizException(code, errorMsg);
        }
    }

    public static void notEmpty(Map map, String template, Object... params) {
        if (map == null || map.isEmpty()) {
            throw new BizException(format(template, params));
        }
    }

    public static void notEmpty(Map map, int code, String template, Object... params) {
        if (map == null || map.isEmpty()) {
            throw new BizException(code, format(template, params));
        }
    }

    static String format(String template, Object... args) {
        if (args == null || args.length == 0) {
            return template;
        } else {
            template = String.valueOf(template);
            StringBuilder builder = new StringBuilder(template.length() + 16 * args.length);
            int templateStart = 0;

            int i;
            int placeholderStart;
            for (i = 0; i < args.length; templateStart = placeholderStart + 2) {
                placeholderStart = template.indexOf("{}", templateStart);
                if (placeholderStart == -1) {
                    break;
                }

                builder.append(template.substring(templateStart, placeholderStart));
                builder.append(args[i++]);
            }

            builder.append(template.substring(templateStart));
            if (i < args.length) {
                builder.append(" [");
                builder.append(args[i++]);

                while (i < args.length) {
                    builder.append(", ");
                    builder.append(args[i++]);
                }

                builder.append(']');
            }

            return builder.toString();
        }
    }
}